package com.sky.task;


import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class OrderTask {
    @Autowired
    OrderMapper orderMapper;

    /**
     * 超时订单自动取消
     */
    @Scheduled(cron = "0 * * * * ?")
    public void overtimeOrder(){
        log.info("超时订单自动取消");
        //查询超时订单
        //下单时间   < 现在时间-15
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-15);
        List<Orders> orders = orderMapper.overtimeOrderQuery(Orders.PENDING_PAYMENT,localDateTime);
        if (orders.size() > 0){
            for (Orders order : orders) {
                order.setStatus(Orders.CANCELLED);
                order.setCancelReason("订单超时取消");
                //设置订单的取消时间
                order.setCancelTime(LocalDateTime.now());
            }
        }
    }

    /**
     * 处理每天未手动点击完成的订单,自动设置完成
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void processDeliveryOrder(){
        log.info("处理每天未手动点击完成的订单,自动设置完成");
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-60);
        List<Orders> orders = orderMapper.overtimeOrderQuery(Orders.DELIVERY_IN_PROGRESS,localDateTime);
        if (orders.size() > 0){
            for (Orders order : orders) {
                order.setStatus(Orders.COMPLETED);
            }
        }
    }

}
